function [b, bnl] = bvqxtools_build
% bvqxtools_build  - return the build number of the BVQXtools installation
%
% FORMAT:       [build, bnl] = bvqxtools_build
%
% No input fields.
%
% Output fields:
%
%       b           build number (year/month/day/hour)
%       bnl         Fx3 cell array file filenames, build, and version

% Version:  v0.8b
% Build:    9112303
% Date:     Nov-23 2009, 3:19 AM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% no argument check necessary

% get main build from BVQXtools.m
b = getbuildversion(which('bvqxtools_build'));

% if more outputs required
if nargout > 1
    p = bvqxtools_path;
    toolboxfiles = [ ...
        findfiles(p, '*.m',   'relative='); ...
        findfiles(p, '*.c',   'relative='); ...
        findfiles(p, '*.*ff', 'relative=')];
    bnl = cell(numel(toolboxfiles), 3);
    for fc = 1:size(bnl, 1)
        bnl{fc, 1} = toolboxfiles{fc};
        [bnl{fc, 2:3}] = getbuildversion([p filesep toolboxfiles{fc}]);
    end
end

% sub function to extract build information:
function [b, v] = getbuildversion(filename)
b = str2double(regexprep(grep(filename, ['Bui' 'ld']), ...
    ['^.*Bui' 'ld\:\s*(\d+)\s*$'], '$1'));
if nargout > 1
    v = regexprep(grep(filename, 'Version'), ...
        '^.*Version\:\s*v(\d+\.\d+.*)\s*$', '$1');
    if iscell(v)
        if ~isempty(v)
            v = v{1};
        else
            v = '';
        end
    end
end
